<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="left" label-width="0px" class="login-form" @keyup.enter.native="handleLogin">
      <div style="position:relative;display:flex;margin-left: 20px;">
        <img :src="logo" style="margin-top: 7px;width: 15%;height: 15%;"/>
        <h3 style="margin-top: 20px;margin-left: 10px;overflow: hidden; " class="title">小蜜蜂-查策消息推送平台</h3>
      </div>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" clearable type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon" style="height: 39px;width: 13px;margin-left: 2px;" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" clearable type="password" auto-complete="off" placeholder="密码">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon" style="height: 39px;width: 13px;margin-left: 2px;" />
        </el-input>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住账号</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-dialog :visible.sync="dialogVisible" style="width: 710px;margin: 0 auto">
        <div class="page-slidecode">
          <slide-verify
            ref="slideblock"
            :l="42"
            :r="10"
            :w="310"
            :h="155"
            :slider-text="text"
            :imgs="bgimgs"
            @success="onSuccess"
            @fail="onFail"
            @refresh="onRefresh"
          ></slide-verify>
        </div>
      </el-dialog>
    </div>
    <div class="div_foot">
      <div>粤ICP备18024597号-9</div>
      <div>咨询电话：400-1516-916 &nbsp;&nbsp;&nbsp;&nbsp;E-mail：service@chacewang.com</div>
      <div>地址：广东省深圳市龙华区五和大道星河WORLD二期C座31层</div>
      &copy;2018-{{ nowYear }} 查策网 版权所有
    </div>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import { getAndSetLogo } from '@/utils/utils'
import { Notification } from 'element-ui'

import verifyImage1 from '@/assets/login_images/2.jpg'
import verifyImage2 from '@/assets/login_images/3.jpg'
import verifyImage3 from '@/assets/login_images/4.jpg'
import verifyImage4 from '@/assets/login_images/5.jpg'
import verifyImage5 from '@/assets/login_images/6.jpg'
import verifyImage6 from '@/assets/login_images/7.jpg'
import verifyImage7 from '@/assets/login_images/8.jpg'
import verifyImage8 from '@/assets/login_images/9.jpg'
import verifyImage9 from '@/assets/login_images/10.jpg'
import verifyImage10 from '@/assets/login_images/11.jpg'
import verifyImage11 from '@/assets/login_images/12.jpg'
import Vue from 'vue'
import SlideVerify from 'vue-monoplasty-slide-verify'
Vue.use(SlideVerify)

export default {
  name: 'Login',
  data() {
    return {
      logo: '../../assets/logo/bee-10.png',
      dialogVisible: false,
      isVerify: false,
      verifyTimes: 0,
      bgimgs: [
        verifyImage1, verifyImage2, verifyImage3, verifyImage4, verifyImage5,
        verifyImage6, verifyImage7, verifyImage8, verifyImage9, verifyImage10, verifyImage11
      ], //	如果使用网络图片资源就无需引入
      // bgimgs: [],	//	如果使用网络图片资源就用该值
      text: '向右滑动~',
      loginForm: {
        username: '',
        password: '',
        rememberMe: false
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCookie()
    this.getAndSetLogoKey()
  },
  methods: {
    getAndSetLogoKey() {
      const logo_key = getAndSetLogo()
      this.logo = require('../../assets/logo/' + logo_key)
    },
    getCookie() {
      const username = Cookies.get('username')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? '' : username,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.dialogVisible = true
          if (this.$refs.slideblock) {
            this.$refs.slideblock.reset()
          }
        } else {
          console.log('提交错误!')
          return false
        }
      })
    },
    onSuccess() {
      if (this.$refs.slideblock) {
        this.$refs.slideblock.reset()
      }
      this.dialogVisible = false
      this.$refs.loginForm.validate(valid => {
        const user = { username: this.loginForm.username, password: this.loginForm.password, rememberMe: this.loginForm.rememberMe }
        if (valid) {
          this.loading = true
          if (user.rememberMe) {
            Cookies.set('username', user.username, { expires: 1 })
            Cookies.set('rememberMe', user.rememberMe, { expires: 1 })
          } else {
            Cookies.remove('username')
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch('Login', user).then((res) => {
            if (res.code === 200) {
              this.$message({
                showClose: true,
                type: 'success',
                message: '登录成功!',
                duration: 1500
              })
              this.$router.push({ path: this.redirect || '/' })
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
            Notification.error({
              title: '错误',
              message: '登录失败，请检查账号或密码！'
            })
          })
        } else {
          console.log('提交错误!')
          return false
        }
      })
    },
    onFail() {
      this.verifyTimes += 1
      Notification.error({
        title: '错误',
        message: '登录失败！'
      })
    },
    onRefresh() {
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    background-image:url('../../assets/login_images/1.jpg');
    height: 100%;
  }
  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #707070;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 365px;
    padding: 25px 25px 5px 25px;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
  }
  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  .div_foot {
    color: #ffffff;
    position: absolute;
    text-align: center;
    font-size: 14px;
    line-height: 25px;
    width: 100%;
    bottom: 0;
    padding:0px;
    margin:0px
}
</style>
